<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use DB;
use Mail;
use Gregwar\Captcha\CaptchaBuilder;
use App\Http\Requests\HomeForgetPostRequest;
use App\Http\Requests\ChangePostRequest;
use App\Http\Requests;

use App\Http\Controllers\Controller;

class ChangeController extends Controller
{
	public function getChange(Request $request)
    {
        //显示找回密码页
      $data = $request->only(['id','token']);

        return view('home.login.change',['data'=>$data]);
    }
  public function  postReset(Request $request){
    $this->validate($request, [
    // 'username' => 'required|max:20',
    'password' => 'required',
    'repassword' => 'required|same:password',
    // 'email' => 'required|email',
    // 'phone' => 'required|regex:[1\d{10}]',
    ],[
        // 'username.required'=>'用户名不能为空',
        // 'username.max'=>'用户名过长',
        'password.required'=>'用户密码不能为空',
        'repassword.required'=>'确认密码不能为空',
        'repassword.same'=>'两次密码不一致',
        // 'email.required'=>'邮箱不能为空',
        // 'email.email'=>'邮箱格式不正确',
        // 'phone.required'=>'手机号不能为空', 
        // 'phone.regex'=>'请填写正确的手机号', 
    ]); 

    //上面的验证都成功后
    //提取部分数据
    $data = $request->only('password');

    // dd($data);
    // $res = DB::table('user')->where('id',$request->input('id'))->first();
    $id = DB::table('user')->where('id',$request->input('id'))->first();
    // 判断id和token是否在数据库对应存在
    if($id->token == $request->input('token')){
      //随机token
        $data['token'] = str_random(50);
        $data['password'] = Hash::make($data['password']);

      //执行密码重置
      $res = DB::table('user')
                    ->where('id',$request->input('id'))
                    ->update($data);

      return redirect('/login')->with('success','密码重置成功');
    }else{
      return back()->with('error','非法操作');
    }
  }
  
   
 
}

   

  
